package com.longrise.android.byjk.plugins.aboutme.personalInfo.person_honor;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.PersonalInfoEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/7.
 * 个人荣誉展示页
 */

public class PersonHonorShowActivity extends BaseActivity2<PersonHonorShowPresenter> implements PersonHonorShowContract.View,
                                PersonHonorAdapter.OnItemClickListener,View.OnClickListener {

    public static final String TAG = "PersonHonorShowActivity";
    private RecyclerView mRcv;
    private Button mBtn;
    private PersonHonorAdapter mAdapter;
    private LinearLayout mllHonor;
    private List<PersonHonorBean> mSelectedHonorBeans = new ArrayList<>();
    private List<PersonHonorBean> mHonorBeanList ;
    private boolean isChecked;
    private List<String> honorIds;
    private RelativeLayout mRlHonorList;
    private Button mBtnAdd;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_person_honor;
    }

    @Override
    public void initView() {
        mRlHonorList = (RelativeLayout) findViewById(R.id.rl_person_honor_rcv);
        mRcv = (RecyclerView) findViewById(R.id.rcv_person_honor);
        mBtn = (Button) findViewById(R.id.btn_honor_add_again);
        mBtnAdd = (Button) findViewById(R.id.btn_person_honor_add);
        mllHonor= (LinearLayout) findViewById(R.id.ll_person_honor_null);
        EventBus.getDefault().register(this);
        setToolbarTitle(AppUtil.getString(R.string.person_honor));
        initRcv();
        initEvent();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
        PersonalInfoEvent event = new PersonalInfoEvent(false);
        event.setmIsUpdateMyMedal(true);
        EventBus.getDefault().post(event);
    }

    private void initRcv() {
        View footerView = View.inflate(mContext, R.layout.item_person_honor_footer, null);
        mAdapter = new PersonHonorAdapter();
        mAdapter.addFooterView(footerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRcv.setLayoutManager(layoutManager);
        mRcv.setAdapter(mAdapter);
        mPresenter.getPersonHonorData();
    }

    public void initEvent(){
        mBtn.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            //点击马上添加
            case R.id.btn_person_honor_add:
                startActivity(AddPersonHonorActivity.class);
                break;

            //点击继续添加
            case R.id.btn_honor_add_again:
                startActivity(AddPersonHonorActivity.class);
                break;

        }
    }

    @Override
    public void refreshData(List<PersonHonorBean> mHonorBeanList) {
        this.mHonorBeanList = mHonorBeanList;
        PrintLog.e(TAG,"mHonorBeanList=:"+mHonorBeanList.toString());
        if(mHonorBeanList.size() != 0){
            mAdapter.setNewData(mHonorBeanList);
            mRlHonorList.setVisibility(View.VISIBLE);
            mllHonor.setVisibility(View.GONE);
            if(mHonorBeanList.size()>=5){
                mBtn.setVisibility(View.GONE);
            }else{
                mBtn.setVisibility(View.VISIBLE);
            }
            UserInfor.getInstance().setUserGRRY(mHonorBeanList.size()+"个");
            setToolbarRightTextTitle(AppUtil.getString(R.string.person_honor_edit));
        }else {
            mllHonor.setVisibility(View.VISIBLE);
            mRlHonorList.setVisibility(View.GONE);
            mAdapter.setNewData(null);
            setToolbarRightTextTitle("");
            UserInfor.getInstance().setUserGRRY("未填写");
        }
    }

    @Override
    public void responseDelteData(EntityBean bean) {

    }


    @Override
    public void onToolbarRightTvClick() {
        super.onToolbarRightTvClick();
        startActivity(PersonHonorEditActivity.class);
    }

    @Override
    public void onItemClick(PersonHonorBean honorBean) {
        String honordesc = honorBean.getHonordesc();
        String honorId = honorBean.getHonorId();
        //跳转到个人荣誉编辑页面
        Intent intent = new Intent(this,AddPersonHonorActivity.class);
        intent.putExtra(AddPersonHonorActivity.HONORDATA,honordesc);
        intent.putExtra(AddPersonHonorActivity.HONORDATAID,honorId);
        startActivity(intent);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshHonorEvent(PersonHonorEvent event) {
        if (event.isRefresh()) {
            mPresenter.getPersonHonorData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            PersonalInfoEvent event1 = new PersonalInfoEvent(false);
            event1.setmIsUpdateMyMedal(true);
            EventBus.getDefault().post(event1);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
